import java.util.Stack;

class Solution {
    public boolean isValid(String s) {
        Stack<Character> characterStack = new Stack<>();
        for(int i = 0;i < s.length(); i++) {
            char ch1 = s.charAt(i);
            if(ch1 == '('||ch1 == '{' || ch1 == '[') {
                characterStack.push(ch1);
            } else {
                if(characterStack.empty()) {
                    return false;
                }
                char ch2 = characterStack.peek();
                if(ch2 == '('&&ch1 == ')' ||ch2 == '{'&&ch1 == '}' ||ch2 == '['&&ch1 == ']') {
                    characterStack.pop();
                } else {
                    return false;
                }
            }
        }
        if(characterStack.empty()) {
            return true;
        }
        return false;
    }
}
